
def count_people(sentence):
    import re
    total = 0
    def chinese_to_int(s):
        if s.isdigit():
            return int(s)
        ch = {'零':0,'一':1,'二':2,'两':2,'三':3,'四':4,'五':5,'六':6,'七':7,'八':8,'九':9}
        if not s:
            return 0
        if '十' in s:
            parts = s.split('十')
            left = parts[0]
            right = parts[1] if len(parts) > 1 else ''
            val = 0
            if left == '':
                val += 10
            else:
                val += ch.get(left,0) * 10
            if right != '':
                val += ch.get(right,0)
            return val
        return ch.get(s, 0)
    # 1) 处理 “A班和B班各N人” 之类的结构
    pattern_each = re.compile(r'([一二三四五六七八九十\d]+班(?:[和、,]?[一二三四五六七八九十\d]+班)*)各([一二三四五六七八九十\d]+)人')
    for m in pattern_each.finditer(sentence):
        classes_part = m.group(1)
        num_each = chinese_to_int(m.group(2))
        classes = re.findall(r'([一二三四五六七八九十\d]+)班', classes_part)
        total += len(classes) * num_each
    sentence = pattern_each.sub('', sentence)

    # 2) 处理 “X班Y人”（班后直接跟人数）
    pattern_class_num = re.compile(r'([一二三四五六七八九十\d]+)班([一二三四五六七八九十\d]+)人')
    for m in pattern_class_num.finditer(sentence):
        num = chinese_to_int(m.group(2))
        total += num
    sentence = pattern_class_num.sub('', sentence)

    # 3) 处理剩余的 “N人”（纯数字或中文数字）
    pattern_num = re.compile(r'([一二三四五六七八九十\d]+)人')
    for m in pattern_num.finditer(sentence):
        total += chinese_to_int(m.group(1))

    return total


